package com.qianxun.modular.system;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;


/**
 * @author Admin
 */
@Controller
public class AdminAction {
    @Value("${sys.maps.admin-username}")
    private String username = "admin";
    @Value("${sys.maps.admin-password}")
    private String password = "admin0357";

    @RequestMapping(value = {"/toLogin"})
    public ModelAndView toLogin(ModelAndView modelAndView){
        modelAndView.setViewName("/login.html");
        return modelAndView;
    }

    @RequestMapping(value = {"/login"})
    public ModelAndView login(@RequestParam String username , @RequestParam String password
            , ModelAndView modelAndView , HttpServletRequest request){
        if(this.username.equals(username) && this.password.equals(password)){
            modelAndView.setViewName("redirect:/toIndex");
            request.getSession(true).setAttribute("logined" ,true);
        }else{
            modelAndView.setViewName("/login.html");
        }
        return modelAndView;
    }

    @RequestMapping(value = {"/toIndex"})
    public ModelAndView index(HttpServletRequest request, ModelAndView modelAndView){
        Object obj = request.getSession(true).getAttribute("logined");
        if(obj == null ){
            modelAndView.setViewName("redirect:/toLogin");
        }else{
            modelAndView.setViewName("redirect:/forward/project");
        }
        return modelAndView;
    }

    @RequestMapping(value = "/forward/{uri}")
    public ModelAndView admin(@PathVariable("uri") String uri ,HttpServletRequest request, ModelAndView modelAndView) {
        Object obj = request.getSession(true).getAttribute("logined");
        if(obj == null ){
            modelAndView.setViewName("redirect:/toLogin");
        }else{
            modelAndView.setViewName("/" + uri + ".html");
        }
        return modelAndView;
    }
}
